package org.jeecg.modules.business.takestockplan.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.material.service.IBasicMaterialService;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.takestockplan.entity.TakeStockPlan;
import org.jeecg.modules.business.takestockplan.entity.constant.TakeStockPlanStatusConstant;
import org.jeecg.modules.business.takestockplan.service.ITakeStockPlanService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 盘点计划
 * @Author: jeecg-boot
 * @Date:   2023-08-02
 * @Version: V1.0
 */
@Api(tags="盘点计划")
@RestController
@RequestMapping("/business/takeStockPlan")
@Slf4j
public class TakeStockPlanController extends JeecgController<TakeStockPlan, ITakeStockPlanService> {
	@Autowired
	private ITakeStockPlanService takeStockPlanService;
	 @Autowired
	 private IWmsRepertoryService repertoryService;

	 @Autowired
	 private IBasicMaterialService materialService;

	 /**
	  * 分页列表查询
	  *
	  * @param takeStockPlan
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="盘点计划-分页列表查询", notes="盘点计划-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<Page<TakeStockPlan>> queryPageList(TakeStockPlan takeStockPlan,
													   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   HttpServletRequest req) {
		 Result<Page<TakeStockPlan>> result = new Result<>();
		 Page<TakeStockPlan> pageList = new Page<>(pageNo,pageSize);
		 pageList = takeStockPlanService.listByPage(pageList, takeStockPlan);
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return result;
	 }
	
	/**
	 *   添加
	 *
	 * @param takeStockPlan
	 * @return
	 */
	@AutoLog(value = "盘点计划-添加")
	@ApiOperation(value="盘点计划-添加", notes="盘点计划-添加")
	@RequiresPermissions("business:takeStockPlan:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody TakeStockPlan takeStockPlan) {
		takeStockPlanService.save(takeStockPlan);
		materialService.updateUsedState(takeStockPlan.getMaterialId());
		return Result.OK("添加成功！", takeStockPlan.getId());
	}
	
	/**
	 *  编辑
	 *
	 * @param takeStockPlan
	 * @return
	 */
	@AutoLog(value = "盘点计划-编辑")
	@ApiOperation(value="盘点计划-编辑", notes="盘点计划-编辑")
	@RequiresPermissions("business:takeStockPlan:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TakeStockPlan takeStockPlan) {
		takeStockPlanService.updateById(takeStockPlan);
		materialService.updateUsedState(takeStockPlan.getMaterialId());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "盘点计划-通过id删除")
	@ApiOperation(value="盘点计划-通过id删除", notes="盘点计划-通过id删除")
	@RequiresPermissions("business:takeStockPlan:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		TakeStockPlan byId = takeStockPlanService.getById(id);
		if (TakeStockPlanStatusConstant.SUBMITTED.equals(byId.getDocumentStatus())) {
			throw new JeecgBootException("单据状态为已提交，不能删除");
		}
		takeStockPlanService.removeById(id);
		materialService.updateUsedState(byId.getMaterialId());
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "盘点计划-批量删除")
	@ApiOperation(value="盘点计划-批量删除", notes="盘点计划-批量删除")
	@RequiresPermissions("business:takeStockPlan:delete")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<TakeStockPlan> takeStockPlans = takeStockPlanService.listByIds(Arrays.asList(ids.split(",")));
		this.takeStockPlanService.removeByIds(Arrays.asList(ids.split(",")));
		for (TakeStockPlan stockPlan : takeStockPlans) {
			materialService.updateUsedState(stockPlan.getMaterialId());
		}
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "盘点计划-通过id查询")
	@ApiOperation(value="盘点计划-通过id查询", notes="盘点计划-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TakeStockPlan> queryById(@RequestParam(name="id",required=true) String id) {
		TakeStockPlan takeStockPlan = takeStockPlanService.getById(id);
		if(takeStockPlan==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(takeStockPlan);
	}

	 /**
	  * 查询生产订单号列表
	  *
	  * @return
	  */
	 @ApiOperation(value="盘点计划-查询生产订单号列表", notes="盘点计划-查询生产订单号列表")
	 @GetMapping(value = "/queryProductOrderCode")
	 public Result<List<String>> queryProductOrderCode() {
		 List<WmsRepertory> list = repertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
				 .isNotNull(WmsRepertory::getProductCode)
				 .ne(WmsRepertory::getProductCode, "")
				 .groupBy(WmsRepertory::getProductCode));
		 List<String> collect = list.stream().map(WmsRepertory::getProductCode).collect(Collectors.toList());
		 return Result.OK(collect);
	 }

	 /**
	  * 根据仓库组查询仓库
	  *
	  * @return
	  */
	 @ApiOperation(value="盘点计划-根据仓库组查询仓库", notes="盘点计划-根据仓库组查询仓库")
	 @GetMapping(value = "/getWarehouseByGroup")
	 public Result<List<WmsBasicWarehouse>> getWarehouseByGroup(String warehouseGroupId) {
		 return Result.OK(takeStockPlanService.getWarehouseByGroup(warehouseGroupId));
	 }

}
